<?php

/**
 * SectionController
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class SectionController extends Zend_Controller_Action {
	protected $_baseUrl;
	protected $_contextPath;
	protected $_utilities;
	protected $_sections;
	protected $_baseLanguage;
	
	public function init() {
		$this->_baseUrl = $this->_request->getBaseURL ();
		$this->_contextPath = "index";
		
		$this->view->assign ( "baseUrl", $this->_baseUrl );
		$this->view->assign ( "contextPath", $this->_contextPath );
		
		$this->_utilities = new Utilities ( );
		$this->view->assign ( "utilities", $this->_utilities );
		
		$section = new SectionModel ( );
		$sections = $section->getAll();
		$this->view->assign ( "sections", $sections );

	}
	
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		$sectionId = (int) $this->_request->getParam ( 'id' );
		$section = new SectionModel ( );
		
		if ($sectionId > 0){
			$aSection = $section->fetchRow ( 'sec_id=' . $sectionId );
			$this->view->assign ( "section", $aSection );
			
			$category = new CategoryModel();
			$categories = $category->getAllCategoryBySectionId($sectionId);
			
			// Lay du lieu default.
			$i=0;
			foreach ($categories as $cat) {
				if ($i==0) {
					$cat_id = $cat->cat_id;
					$cat_name = $cat->cat_name;
				}
				$i++;
			}
			$commodity = new CommodityModel();
			$commodities = $commodity->getCommodityByCatId($cat_id, 30);
			
			$this->view->assign("cat_name", $cat_name);
			$this->view->assign("categories", $categories);
			$this->view->assign("commodities", $commodities);
		}
		
	}
	
	public function viewAction() {
		$sectionId = $this->_request->getParam ( 's' );
		$categoryId = $this->_request->getParam ( 'c1' );
		$commodId = $this->_request->getParam ( 'c2' );
		$section = new SectionModel ( );
		
		if ($sectionId != null) {
			$aSection = $section->fetchRow ( 'sec_id=' . $sectionId );
			$this->view->assign ( "section", $aSection );
		}
	}
}
